# encoding:utf-8
"""
@file = log
@author = Administrator
@create_time = 2022/8/21- 10:03

"""
from login.models import ExecLog, LoginLog


def xss_escape(s: str):
    if s is None:
        return None
    else:
        return s.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;").replace("'", "&#39;").replace('"', "&#34;")


def login_log(request, uid, is_access, desc):
    info = {
        'method': request.method,
        'url': request.path,
        'ip': request.META.get('REMOTE_ADDR'),
        'user_agent': xss_escape(request.headers.get('User-Agent')),
        'desc': desc,
        'uid': uid,
        'success': int(is_access)

    }
    log = LoginLog(
        url=info.get('url'),
        ip=info.get('ip'),
        user_agent=info.get('user_agent'),
        desc=info.get('desc'),
        uid=info.get('uid'),
        method=info.get('method'),
        success=info.get('success')
    )
    log.save()
    return log


def exec_log(request, is_access, desc):
    user_id = request.session.get('user_id')
    # desc = str(dict(request.values).get('Params'))

    info = {
        'method': request.method,
        'url': request.path,
        'ip': request.META.get('REMOTE_ADDR'),
        'user_agent': xss_escape(request.headers.get('User-Agent')),
        'desc': desc,
        'uid': user_id,
        'success': int(is_access)

    }
    log = ExecLog(
        url=info.get('url'),
        ip=info.get('ip'),
        user_agent=info.get('user_agent'),
        desc=info.get('desc'),
        uid=info.get('uid'),
        method=info.get('method'),
        success=info.get('success')
    )
    log.save()

    return log
